package com.example.springproject.xuwsh.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.springproject.xuwsh.domain.entry.UserEntry;
import com.example.springproject.xuwsh.mapper.UserMapper;
import com.example.springproject.xuwsh.mapper.UserMapperX;
import com.example.xuwsh.myBatisPlus.core.query.JuXiLambdaQueryWrapper;
import com.example.xuwsh.myBatisPlus.utils.JuXiMapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author: xuwsh
 * @Date: 2023/09/19/10:19
 * @Description:
 */
@Service
public class UserServiceImpl {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserMapperX userMapperX;

    /**
     * 查询用户所有
     *
     * @return
     */
    public List<UserEntry> selectUser() {
        return userMapper.selectList();
    }

    /**
     * 添加用户
     *
     * @param userEntry
     */
    public void addUser(UserEntry userEntry) {
        userMapper.insert(userEntry);
    }

    /**
     * 批量添加用户
     *
     * @param userEntryList
     */
    public void addUserList(List<UserEntry> userEntryList) {
        userMapper.insertBatchSomeColumn(userEntryList);
    }

    /**
     * 根据id去修改数据
     *
     * @param entry
     */
    public void updateById(UserEntry entry) {
        userMapper.updateById(entry);
    }

//    /**
//     * 更新实体类，不建议使用
//     *
//     * @param userEntry
//     */
//    public void updateEntry(UserEntry userEntry) {
//        userMapper.update(userEntry);
//    }

    /**
     * 修改并新增
     *
     * @param userEntry
     */
    public void saveOrUpdateBatch(List<UserEntry> userEntry) {
        userMapper.saveOrUpdateBatch(userEntry);
    }

    /**
     * 批量修改
     *
     * @param userEntryList
     */
    public void updateList(List<UserEntry> userEntryList) {
        userMapper.updateByIdList(userEntryList);
    }

    /**
     * 它的作用和delete的作用是一样的。底层逻辑一样
     *
     * @param columnMap
     */
    public void deleteMap(Map<String, Object> columnMap) {
        userMapper.deleteByMap(columnMap);
    }

    /**
     * 删除
     */
    public void delete() {
        LambdaQueryWrapper<UserEntry> eq = new LambdaQueryWrapper<UserEntry>().eq(UserEntry::getUserName, "风鹰铠甲").eq(UserEntry::getUserAge, 24L);
        userMapper.delete(eq);
    }

    /**
     * 查询一条数据
     *
     * @param value
     * @return
     */
    public Object selectOne(String value) {
        return userMapper.selectOne(UserEntry::getUserName, value, Boolean.FALSE);
    }

    public Object selectOne(UserEntry userEntry) {
        JuXiMapUtils<UserEntry> mapUtils = new JuXiMapUtils<>();
        mapUtils.put(UserEntry::getUserName, userEntry.getUserName());
        return userMapper.selectOne(mapUtils.getMap());
    }

    public Object exists(String value) {
        return userMapper.exists(UserEntry::getUserName, value);
    }

    public Object selectCount(String value) {
        return userMapper.selectCount(UserEntry::getUserName, value);
    }

    public Object selectList() {
        return userMapper.selectList();
    }

    public Object selectList(String value) {
        return userMapper.selectList(UserEntry::getUserName, value);
    }

    public Object selectListRange(int lValue, int rValue) {
        return userMapper.selectListRange(UserEntry::getUserAge, lValue, rValue);
    }

    public Object selectIsNull() {
//        UserEntry userEntry = new UserEntry();
//        return userMapper.selectUserAgeIsNull(userEntry);
        return null;
    }

    public Object selectEqIfParent() {
        UserEntry userEntry = new UserEntry();
        userEntry.setUserName("炎龙铠甲");
        userEntry.setUserAge(34);
        List<UserEntry> userEntries = userMapperX.selectByUserName(userEntry);
        return userEntries;
    }

    public void updateUserName(UserEntry userEntry) {
        userMapperX.updateUserName(userEntry);
    }

    public Object selectNeIfPresent(UserEntry userEntry) {
        return userMapperX.selectNeIfPresent(userEntry);
    }

    public List<UserEntry> selectListMap(Map<String, Object> map) {
        return userMapper.selectList(map);
    }

    public List<UserEntry> selectListIn(Map<String, Object> map) {
        return userMapper.selectListIn(map);
    }

    public void test() {
        JuXiLambdaQueryWrapper<UserEntry> queryWrapper = new JuXiLambdaQueryWrapper<>();
        queryWrapper.likeIfPresent(UserEntry::getUserName, "黑犀");
        List<UserEntry> userEntries = userMapper.selectList(queryWrapper);
        System.out.println(userEntries);
    }

    public void updateEntry(UserEntry userEntry) {
        userMapper.updateEntryEq(userEntry, UserEntry::getUserName, userEntry.getUserName());
    }

    public void updateFiledEq(UserEntry userEntry, Map<String, Object> map) {
        userMapper.updateFiledEq(map, UserEntry::getUserName, userEntry.getUserName());
    }
}
